package com.fand.platform.authority.controller;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.platform.authority.dto.RoleDto;
import com.fand.platform.authority.dto.RoleReOpAcDto;
import com.fand.platform.authority.service.AuthorityService;
import com.fand.platform.authority.validator.RoleReOpAcValidator;
import com.fand.platform.authority.validator.RoleValidator;
import com.fand.platform.authority.vo.RoleVo;
import com.google.common.collect.Maps;

/**
 * 角色
 * 
 * @author yaoHT
 */
@Controller
public class RoleController {
	@Qualifier(AuthorityService.BEAN_ID)
	@Autowired(required = false)
	private AuthorityService authorityService;

	@Qualifier(RoleValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator roleValidator;

	@Qualifier(RoleReOpAcValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator roleReOpAcValidator;

	@RequestMapping(value = { "/admin/authority/role/role/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminRoleDelete(final RoleVo roleVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getRoleService().deleteRoleBy2(roleVo.getRole());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/authority/role/role/edit.html" }, method = RequestMethod.GET)
	public String adminRoleEdit(final RoleVo roleVo, final Model model) {
		final RoleDto role = this.authorityService.getRoleService().getRoleBy2(roleVo.getRole());

		model.addAttribute("role", role);

		return "/admin/authority/role/role/edit";
	}

	@RequestMapping(value = { "/admin/authority/role/role.html", "/admin/authority/role/role/index.html" })
	public String adminRoleIndex() {
		return "/admin/authority/role/role/index";
	}

	@RequestMapping(value = { "/admin/authority/role/role/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminRoleIndexPagination(final RoleVo roleVo) {
		final PaginationDto<Map<String, Object>> rolePagination = this.authorityService.getRoleService()
				.paginationRoleBy1ForMapList(roleVo.getRole());

		final Map<String, Object> rolePaginationMap = Maps.newHashMap();

		rolePaginationMap.put("total", rolePagination.getTotalSize());
		rolePaginationMap.put("rows", rolePagination.getDataList());

		return rolePaginationMap;
	}

	@RequestMapping(value = { "/admin/authority/role/role/new.html" }, method = RequestMethod.GET)
	public String adminRoleNew(final RoleVo roleVo, final Model model) {
		model.addAttribute("role", roleVo.getRole());

		return "/admin/authority/role/role/new";
	}

	@RequestMapping(value = { "/admin/authority/role/roleReOpAc/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminRoleReOpAcDelete(final RoleVo roleVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getRoleService().deleteRoleReOpAcBy2(roleVo.getRoleReOpAc());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/authority/role/roleReOpAc/edit.html" }, method = RequestMethod.GET)
	public String adminRoleReOpAcEdit(final RoleVo roleVo, final Model model) {
		final RoleReOpAcDto roleReOpAc = this.authorityService.getRoleService().getRoleReOpAcBy2(
				roleVo.getRoleReOpAc());

		model.addAttribute("roleReOpAc", roleReOpAc);

		return "/admin/authority/role/roleReOpAc/edit";
	}

	@RequestMapping(value = { "/admin/authority/role/roleReOpAc.html", "/admin/authority/role/roleReOpAc/index.html" })
	public String adminRoleReOpAcIndex() {
		return "/admin/authority/role/roleReOpAc/index";
	}

	@RequestMapping(value = { "/admin/authority/role/roleReOpAc/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminRoleReOpAcIndexPagination(final RoleVo roleVo) {
		final PaginationDto<Map<String, Object>> roleReOpAcPagination = this.authorityService.getRoleService()
				.paginationRoleReOpAcBy1ForMapList(roleVo.getRoleReOpAc());

		final Map<String, Object> roleReOpAcPaginationMap = Maps.newHashMap();

		roleReOpAcPaginationMap.put("total", roleReOpAcPagination.getTotalSize());
		roleReOpAcPaginationMap.put("rows", roleReOpAcPagination.getDataList());

		return roleReOpAcPaginationMap;
	}

	@RequestMapping(value = { "/admin/authority/role/roleReOpAc/new.html" }, method = RequestMethod.GET)
	public String adminRoleReOpAcNew(final RoleVo roleVo, final Model model) {
		model.addAttribute("roleReOpAc", roleVo.getRoleReOpAc());

		return "/admin/authority/role/roleReOpAc/new";
	}

	@RequestMapping(value = { "/admin/authority/role/roleReOpAc/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminRoleReOpAcSave(final RoleVo roleVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.roleReOpAcValidator.validate(roleVo.getRoleReOpAc(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.authorityService.getRoleService().saveRoleReOpAcBy1ForRoleReOpAc(roleVo.getRoleReOpAc());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/authority/role/roleReOpAc/validationBy1.json" })
	public @ResponseBody
	Boolean adminRoleReOpAcValidationBy1(final RoleVo roleVo) {
		try {
			final int count = this.authorityService.getRoleService().readRoleReOpAcBy1ForInteger(
					roleVo.getRoleReOpAc());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/authority/role/roleReOpAc/view.html" }, method = RequestMethod.GET)
	public String adminRoleReOpAcView(final RoleVo roleVo, final Model model) {
		final RoleReOpAcDto roleReOpAc = this.authorityService.getRoleService().getRoleReOpAcBy2(
				roleVo.getRoleReOpAc());

		model.addAttribute("roleReOpAc", roleReOpAc);

		return "/admin/authority/role/roleReOpAc/view";
	}

	@RequestMapping(value = { "/admin/authority/role/role/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminRoleSave(final RoleVo roleVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.roleValidator.validate(roleVo.getRole(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.authorityService.getRoleService().saveRoleBy1ForRole(roleVo.getRole());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/authority/role/role/validationBy1.json" })
	public @ResponseBody
	Boolean adminRoleValidationBy1(final RoleVo roleVo) {
		try {
			final int count = this.authorityService.getRoleService().readRoleBy1ForInteger(roleVo.getRole());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/authority/role/role/view.html" }, method = RequestMethod.GET)
	public String adminRoleView(final RoleVo roleVo, final Model model) {
		final RoleDto role = this.authorityService.getRoleService().getRoleBy2(roleVo.getRole());

		model.addAttribute("role", role);

		return "/admin/authority/role/role/view";
	}
}
